package fw.qaz.asmx.reader;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.readystatesoftware.systembartint.SystemBarTintManager;

import fw.qaz.asmx.reader.adapter.VPAdapter;
import fw.qaz.asmx.reader.fragment.BookShelfFragment;
import fw.qaz.asmx.reader.fragment.FileBrowseFragment;
import fw.qaz.asmx.reader.fragment.ReadFragment;
import fw.qaz.asmx.reader.fragment.SettingsFragment;

public class MainActivity extends AppCompatActivity
{
    private Toolbar toolbar;
    private TabLayout tabs;

    private ViewPager viewPager;
    private VPAdapter vpAdapter;

    private FrameLayout fl;

    private SystemBarTintManager tintManager; /* KitKat's TintManager */

    @Override protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle(R.string.app_name);
        toolbar.setLogo(R.mipmap.logo_toolbar);
        tabs = (TabLayout) findViewById(R.id.tabs);

        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT)
        {   /* KitKat's Tint Settings */
            tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setNavigationBarTintEnabled(true);
            tintManager.setTintColor(Color.parseColor("#ff80cbc4"));
        }

        fl = (FrameLayout) findViewById(R.id.fl);
        viewPager = (ViewPager) findViewById(R.id.view_pager);
        vpAdapter = new VPAdapter(getSupportFragmentManager());
        vpAdapter.addFragment(new BookShelfFragment(), "我的书架");
        vpAdapter.addFragment(new FileBrowseFragment(), "文件浏览");
        vpAdapter.addFragment(new SettingsFragment(), "阅读设置");
        viewPager.setAdapter(vpAdapter);
        tabs.setupWithViewPager(viewPager);
        tabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
        {
            @Override public void onTabSelected(TabLayout.Tab tab)
            {
            }

            @Override public void onTabUnselected(TabLayout.Tab tab)
            {
            }

            @Override public void onTabReselected(TabLayout.Tab tab)
            {
                switch(tab.getPosition())
                {
                case 0:
                    //刷新书架
                    break;
                case 2:
                    //读取设置
                    break;
                }
            }
        });
    }

    /**
     * call this method and pass file,progress to open ReadFragment.
     *
     * @param file     Txt file's full path.
     * @param progress Txt file's char offset.
     */
    public void startRead(String file, int progress)
    {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
                             WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        stopTint();
        switchUI();
        //
        ReadFragment rf = new ReadFragment();
        Bundle b = new Bundle();
        b.putString("FilePath", file);
        b.putInt("Progress", progress);
        rf.setArguments(b);
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fl, rf)
                .addToBackStack(null).commit();
    }

    private void startTint()
    {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT)
        {   /* KitKat's Tint Settings */
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setNavigationBarTintEnabled(true);
        }
    }

    private void stopTint()
    {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT)
        {   /* KitKat's Tint Settings */
            tintManager.setStatusBarTintEnabled(false);
            tintManager.setNavigationBarTintEnabled(false);
        }
    }

    private void switchUI()
    {
        if (toolbar.getVisibility() == View.VISIBLE)
        {
            toolbar.setVisibility(View.GONE);
            tabs.setVisibility(View.GONE);
            viewPager.setVisibility(View.GONE);
            fl.setVisibility(View.VISIBLE);
        }
        else
        {
            fl.setVisibility(View.GONE);
            toolbar.setVisibility(View.VISIBLE);
            tabs.setVisibility(View.VISIBLE);
            viewPager.setVisibility(View.VISIBLE);
        }
    }

    private long lastTime = 0;/* save last click BACK key's unix epoch */

    @Override public boolean onKeyUp(int kc, @NonNull KeyEvent event)
    {
        switch (kc)
        {
        case KeyEvent.KEYCODE_BACK:
            if (getSupportFragmentManager().getBackStackEntryCount() > 0)
            {
                toolbar.setVisibility(View.VISIBLE);
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
                                       WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                getSupportFragmentManager().popBackStack();
                startTint();
                switchUI();
                return false;
            }
            long cur = System.currentTimeMillis();
            if (cur - lastTime <= 850L)
            {
                finish();
            }
            else
            {
                lastTime = cur;
                Toast.makeText(this, R.string.double_click_back_to_exit, Toast.LENGTH_SHORT)
                     .show();
            }
            return false;
        }
        return super.onKeyUp(kc, event);
    }
}
